 // pages/luckyStar/luckyStar.js
const app = getApp()
var url = require('../../utils/url.js');
var page = 1;
var last_page = 0;
var user_id;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'goods/treasure/goods',
      data: { page: page },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res.data)
        var shopss = res.data.data.data;
        var bill = "bill"
        for (var i = 0; i < shopss.length; i++) {
          // console.log(i)
          var billF = (shopss[i].goods_salenum) / (shopss[i].goods.goods_storage) * 100;
          shopss[i].bill = billF.toFixed(2) + "%" 
        }
        wx.hideLoading();
        that.setData({
          shops: that.data.shops.concat(shopss)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    nav: [],
    news: [],
    shops: [],
    indicatorDots: true,  //小点
    indicatorColor: "white",
    autoplay: true,  //是否自动轮播
    interval: 3000,  //间隔时间
    duration: 500,  //滑动时间
    indicatorActiveColor: "#DD2626",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    console.log(options)
    if (options.userId){
      wx.setStorage({
        key: 'rec_id',
        data: options.userId,
      })
    }
     that.getNav()
     that.getAds()
     that.getGoods()
     that.getAwardLog()
  },
  getGoods(){
    var that = this;
    if (page > 1) {
      page--;
    }
    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'goods/treasure/goods',
      data: { page: page },
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res)
        var msg = res.data.msg;
        wx.hideLoading()
        if (res.data.code == 200) {
          last_page = res.data.data.last_page
          var shops = res.data.data.data;
          var bill="bill"
          for(var i=0;i<shops.length;i++){
            // console.log(i)
            var billF = (shops[i].goods_salenum) / (shops[i].goods.goods_storage) * 100; 
            shops[i].bill = billF.toFixed(2)+"%"                                                                    
          }
          // console.log(shops)
          that.setData({
            shops: shops
          })
         
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getNav(){
    var that=this;
    wx.request({
      url: url.URl + 'goods/treasure/nav',
      data: {},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
           that.setData({
             nav:res.data.data
           })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getAwardLog: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: url.URl + 'goods/treasure/award/log',
      data: {},
      method: 'get',
      success: function (res) {
        // console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {
          that.setData({
            news:res.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getAds(){
    var that = this;
    wx.request({
      url: url.URl + 'goods/treasure/ads',
      data: {},
      method: 'get',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        // console.log(res)
        var msg = res.data.msg;
        if (res.data.code == 200) {
          that.setData({
            banner: res.data.data
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  toSearch(){
    wx.navigateTo({
      url: '/pages/searchList/searchList?type=2',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        user_id=res.data.id;
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
     
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var self = this;
    setTimeout(() => {
      // 模拟请求数据，并渲染
      page=1;
      self.getGoods()
      // 数据成功后，停止下拉刷新
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // console.log(page)
    var that = this;
    if (that.data.index != 0) {
      loadMore(that)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var row = that.data.row;
    if (res.from === 'button') {

    }
    return {
      title: "积分幸运星",
      path: '/pages/luckyStar/luckyStar?userId=' + user_id,
      success: function (res) {
        // console.log('成功', res)
      }
    }
  }
})